package com.dq.service.set.impl;

import com.dq.context.Constant;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.GiftSetQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.AbstractEntity;
import com.dq.domain.base.impl.BaseDaoImpl;
import com.dq.domain.org.Org;
import com.dq.domain.setup.GiftGrantSet;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.set.GiftGrantSetService;
import com.dq.utils.BeanNullUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author luchangtong
 * @Date 2019/12/27 16:17
 */
@Service
public class GiftGrantSetServiceImpl extends BaseRepositories implements GiftGrantSetService {
    @Override
    public Vo list(GiftSetQuery giftSetQuery) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        if(sysUser == null){
            throw new AdminException("请先登录");
        }
        giftSetQuery.format();
        Map<String, Object> query = giftSetQuery.parseMap();
        if(StringUtils.isNotEmpty(giftSetQuery.getCategoryName())){
            query.put("categoryName"+ BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(giftSetQuery.getCategoryName()));
            query.remove("categoryName");
        }
        if(giftSetQuery.getBeginTime()!=null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr,giftSetQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(sysUser.getSection() != null){
            query.put("org_id",sysUser.getSection().getId());
            query.remove("orgId");
        }
        if(giftSetQuery.getEndTime()!=null){
            query.put("createTime"+ BaseDaoImpl.lteSqlStr,giftSetQuery.getEndTime());
            query.remove("endTime");
        }
        List<GiftGrantSet> giftGrantSets = dao.find(dao.query2Hql(GiftGrantSet.class,query),query);
        Long count = dao.count(dao.count2Hql(GiftGrantSet.class,query),query);
        PageHelper.startPage(giftSetQuery.getPage(),giftSetQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<GiftGrantSet.Vo>) VO.toVOCollection(giftGrantSets));
        pageInfo.setTotal(count);
        return new Vo(pageInfo);
    }

    @Override
    public Vo add(GiftGrantSet giftGrantSet) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        giftGrantSet.setOrg((Org) sysUser.getSection());
        giftGrantSet.setGiftCategoryNo(AbstractEntity.generyDatetimeAndRandomNum(Constant.ORDERS_SUFF_COUNT));
        giftGrantSet.setVersion(Constant.VERSION_VALUE);
        giftGrantSet.setDeleted(false);
        giftGrantSet.setCreateTime(System.currentTimeMillis());
        giftGrantSet.setLastModifyTime(System.currentTimeMillis());
        giftGrantSet.setCreateUser(sysUser);
        giftGrantSet.setUpdateUser(sysUser);
        giftGrantSetRepository.save(giftGrantSet);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo edit(Long id, GiftGrantSet giftGrantSetEdit) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        GiftGrantSet giftGrantSet = dao.get(GiftGrantSet.class,id);
        if(giftGrantSet == null){
            throw new AdminException("请重新选择");
        }
        giftGrantSetEdit.setUpdateUser(sysUser);
        giftGrantSetEdit.setLastModifyTime(System.currentTimeMillis());
        BeanUtils.copyProperties(giftGrantSetEdit,giftGrantSet, BeanNullUtil.getNullPropertyNames(giftGrantSetEdit));
        giftGrantSetRepository.save(giftGrantSet);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo delete(Long id) throws AdminException {
        GiftGrantSet giftGrantSet = dao.get(GiftGrantSet.class,id);
        if(giftGrantSet == null){
            throw new AdminException("请重新选择");
        }
        giftGrantSet.setDeleted(true);
        giftGrantSetRepository.save(giftGrantSet);
        return new Vo(HttpStatus.OK);
    }
}
